package com.Business;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;

import com.DAO.*;
import com.Entity.*;

/**
 * Session Bean implementation class AdminSession
 */
@Stateless
@TransactionManagement(javax.ejb.TransactionManagementType.CONTAINER)
public class AdminSession implements AdminSessionRemote {

	/**
	 * Default constructor.
	 */
	public AdminSession() {
		// TODO Auto-generated constructor stub
	}

	@EJB
	private CompanyUserFacade companyUserFacade;
	@EJB
	private ComplaintFacade complaintFacade;
	@EJB
	private TypeOfUserFacade typeOfUserFacade;
	@EJB
	private ArticleCategoryFacade articleCategoryFacade;
	@EJB
	private DepartmentFacade departmentFacade;
	@EJB
	private ComplaintCategoryFacade complaintCategoryFacade;
	@EJB
	private FaqFacade faqFacade;
	@EJB
	private FeedbackFacade feedbackFacade;

	private CompanyUser tempUser;
	private List<CompanyUser> tempUserList;
	private Complaint tempComplaint;
	private List<Complaint> tempComplaintList;
	private List<Article> tempArticleList;
	private TypeOfUser tempTypeOfUser;
	private Department tempDepartment;
	private ComplaintCategory tempComplaintCategory;
	private ArticleCategory tempArticleCategory;
	private Faq tempFaq;
	private Feedback tempFeedback;

	@Override
	public boolean createTypeOfUser(TypeOfUser typeOfUser) throws RemoteException {
		// TODO Auto-generated method stub
		tempTypeOfUser = typeOfUserFacade.find(typeOfUser.getId());
		tempTypeOfUser.setName(typeOfUser.getName());
		typeOfUserFacade.create(tempTypeOfUser);
		return true;
	}

	@Override
	public List<TypeOfUser> retrieveTypeOfUserList() throws RemoteException {
		// TODO Auto-generated method stub
		return typeOfUserFacade.findAll();
	}

	@Override
	public boolean updateTypeOfUser(TypeOfUser typeOfUser) throws RemoteException {
		// TODO Auto-generated method stub
		tempTypeOfUser = typeOfUserFacade.find(typeOfUser.getId());
		tempTypeOfUser.setName(typeOfUser.getName());
		typeOfUserFacade.edit(tempTypeOfUser);
		return true;
	}

	@Override
	public boolean deleteTypeOfUser(int typeOfUserID) throws RemoteException {
		// TODO Auto-generated method stub
		tempTypeOfUser = typeOfUserFacade.find(typeOfUserID);
		typeOfUserFacade.remove(tempTypeOfUser);
		return true;
	}

	@Override
	public boolean createDepartment(Department department) throws RemoteException {
		// TODO Auto-generated method stub
		tempDepartment = new Department(0);
		tempDepartment.setName(department.getName());
		departmentFacade.create(tempDepartment);
		return true;
	}

	@Override
	public List<Department> retrieveDepartmentList() throws RemoteException {
		// TODO Auto-generated method stub
		return departmentFacade.findAll();
	}

	@Override
	public boolean updateDepartment(Department department) throws RemoteException {
		// TODO Auto-generated method stub
		tempDepartment = departmentFacade.find(department.getId());
		tempDepartment.setName(department.getName());
		departmentFacade.edit(tempDepartment);
		return true;
	}

	@Override
	public boolean deleteDepartment(int departmentID) throws RemoteException {
		// TODO Auto-generated method stub
		tempDepartment = departmentFacade.find(departmentID);
		departmentFacade.remove(tempDepartment);
		return true;
	}

	@Override
	public boolean createUser(CompanyUser companyUser) throws RemoteException {
		// TODO Auto-generated method stub
		tempUser = new CompanyUser(0);
		tempUser.setLoginCode(companyUser.getLoginCode());
		tempUser.setLoginPass(companyUser.getLoginPass());
		tempUser.setFirstName(companyUser.getFirstName());
		tempUser.setLastName(companyUser.getLastName());
		tempUser.setGender(companyUser.getGender());
		tempUser.setEmail(companyUser.getEmail());
		tempUser.setPhoneNumber(companyUser.getPhoneNumber());
		companyUserFacade.create(tempUser);
		return true;
	}

	@Override
	public List<CompanyUser> retrieveUserList(int typeOfUserID) throws RemoteException {
		TypeOfUser typeOfUser = typeOfUserFacade.find(typeOfUserID);
		tempUserList = typeOfUser.getCompanyUserList();
		return tempUserList;
	}

	@Override
	public boolean updateUser(CompanyUser companyUser) throws RemoteException {
		// TODO Auto-generated method stub
		tempUser = companyUserFacade.find(companyUser.getId());
		tempUser.setLoginPass(companyUser.getLoginPass());
		tempUser.setFirstName(companyUser.getFirstName());
		tempUser.setLastName(companyUser.getLastName());
		tempUser.setGender(companyUser.getGender());
		tempUser.setDateOfBirth(companyUser.getDateOfBirth());
		tempUser.setEmail(companyUser.getEmail());
		tempUser.setPhoneNumber(companyUser.getPhoneNumber());
		tempUser.setAddress(companyUser.getAddress());
		tempUser.setAvatar(companyUser.getAvatar());
		companyUserFacade.edit(tempUser);
		return true;
	}

	@Override
	public boolean deleteUser(int userID) throws RemoteException {
		// TODO Auto-generated method stub
		tempUser = companyUserFacade.find(userID);
		companyUserFacade.remove(tempUser);
		return true;
	}

	@Override
	public boolean createComplaintCategory(ComplaintCategory complaintCategory) throws RemoteException {
		// TODO Auto-generated method stub
		tempComplaintCategory = new ComplaintCategory(0);
		tempComplaintCategory.setName(complaintCategory.getName());
		complaintCategoryFacade.create(tempComplaintCategory);
		return true;
	}

	@Override
	public List<ComplaintCategory> retrieveComplaintCategoryList() throws RemoteException {
		// TODO Auto-generated method stub
		return complaintCategoryFacade.findAll();
	}

	@Override
	public boolean updateComplaintCategory(ComplaintCategory complaintCategory) throws RemoteException {
		// TODO Auto-generated method stub
		tempComplaintCategory = complaintCategoryFacade.find(complaintCategory.getId());
		tempComplaintCategory.setName(complaintCategory.getName());
		complaintCategoryFacade.edit(tempComplaintCategory);
		return true;
	}

	@Override
	public boolean deleteComplaintCategory(int complaintCategoryID) throws RemoteException {
		// TODO Auto-generated method stub
		tempComplaintCategory = complaintCategoryFacade.find(complaintCategoryID);
		complaintCategoryFacade.remove(tempComplaintCategory);
		return true;
	}

	@Override
	public List<Complaint> retrieveComplaintList(int status) throws RemoteException {
		// TODO Auto-generated method stub
		tempComplaintList = complaintFacade.getComplaintListByStatus(status);
		return tempComplaintList;
	}

	@Override
	public boolean updateComplaintPriority(int complaintID, int priority) throws RemoteException {
		// TODO Auto-generated method stub
		tempComplaint = complaintFacade.find(complaintID);
		tempComplaint.setPriority(priority);
		complaintFacade.edit(tempComplaint);
		return true;
	}

	@Override
	public boolean deleteComplaint(int complaintID) throws RemoteException {
		// TODO Auto-generated method stub
		tempComplaint = complaintFacade.find(complaintID);
		complaintFacade.remove(tempComplaint);
		return true;
	}

	@Override
	public boolean assignComplaintToTechnician(int complaintID, int technicianID) throws RemoteException {
		// TODO Auto-generated method stub
		tempComplaint = complaintFacade.find(complaintID);
		tempUser = companyUserFacade.find(technicianID);
		tempComplaint.setSolvingTechnicianID(tempUser);
		tempComplaint.setStatus(1);
		complaintFacade.edit(tempComplaint);
		return true;
	}

	@Override
	public boolean createArticleCategory(ArticleCategory articleCategory) throws RemoteException {
		// TODO Auto-generated method stub
		tempArticleCategory = new ArticleCategory(0);
		tempArticleCategory.setName(articleCategory.getName());
		articleCategoryFacade.create(tempArticleCategory);
		return true;
	}

	@Override
	public List<ArticleCategory> retrieveArticleCategoryList() throws RemoteException {
		// TODO Auto-generated method stub
		return articleCategoryFacade.findAll();
	}

	@Override
	public boolean updateArticleCategory(ArticleCategory articleCategory) throws RemoteException {
		// TODO Auto-generated method stub
		tempArticleCategory = articleCategoryFacade.find(articleCategory.getId());
		tempArticleCategory.setName(articleCategory.getName());
		articleCategoryFacade.edit(tempArticleCategory);
		return true;
	}

	@Override
	public boolean deleteArticleCategory(int articleCategoryID) throws RemoteException {
		// TODO Auto-generated method stub
		tempArticleCategory = articleCategoryFacade.find(articleCategoryID);
		articleCategoryFacade.remove(tempArticleCategory);
		return true;
	}

	@Override
	public List<Article> retrieveArticleList(int articleCategoryID) throws RemoteException {
		// TODO Auto-generated method stub
		ArticleCategory articleCategory = articleCategoryFacade.find(articleCategoryID);
		tempArticleList = articleCategory.getArticleList();
		return tempArticleList;
	}

	@Override
	public boolean createFaq(Faq faq) throws RemoteException {
		// TODO Auto-generated method stub
		tempFaq = new Faq(0);
		tempFaq.setQuestion(faq.getQuestion());
		tempFaq.setAnswer(faq.getAnswer());
		faqFacade.create(tempFaq);
		return true;
	}

	@Override
	public List<Faq> retrieveFaqList() throws RemoteException {
		// TODO Auto-generated method stub
		return faqFacade.findAll();
	}

	@Override
	public boolean updateFaq(Faq faq) throws RemoteException {
		// TODO Auto-generated method stub
		tempFaq = faqFacade.find(faq.getId());
		tempFaq.setQuestion(faq.getQuestion());
		tempFaq.setAnswer(faq.getAnswer());
		faqFacade.edit(tempFaq);
		return true;
	}

	@Override
	public boolean deleteFaq(int faqID) throws RemoteException {
		// TODO Auto-generated method stub
		tempFaq = faqFacade.find(faqID);
		faqFacade.remove(tempFaq);
		return true;
	}

	@Override
	public List<Feedback> retrieveFeedbackList(int status) throws RemoteException {
		// TODO Auto-generated method stub
		return feedbackFacade.getFeedbackListByStatus(status);
	}

	@Override
	public Feedback readFeedback(int feedbackID) throws RemoteException {
		// TODO Auto-generated method stub
		tempFeedback = feedbackFacade.find(feedbackID);
		return tempFeedback;
	}

	@Override
	public boolean replyFeedback(int feedbackID) throws RemoteException {
		// TODO Auto-generated method stub
		tempFeedback = feedbackFacade.find(feedbackID);
		//String userEmail = tempFeedback.getCompanyUserID.getId();
		// -------EMAIL---------
		return true;
	}

	@Override
	public List<Complaint> retrieveComplaintListSummary(Date fromLodgingDate, Date toLodgingDate) throws RemoteException {
		// TODO Auto-generated method stub
		tempComplaintList = complaintFacade.getComplaintListByDateRange(fromLodgingDate, toLodgingDate);
		return tempComplaintList;
	}

}
